package core.reports;

import java.io.IOException;
import java.text.DecimalFormat;
import core.Item;
import core.ItemManager;
import core.Product;
import core.ProductGroup;
import core.ProductManager;
import core.RootStorageUnit;
import core.StorageUnit;

/**
 * This implementation of IReportVisitor generates the first part of N Month Supply Report
 * @author Royce
 *
 */
public class NMonthSupplyVisitor1 implements IReportVisitor 
{
	private IReportBuilder _builder;
	private int _months;
	private TraversingType _type;
	private int _currentSupply;
	
	public NMonthSupplyVisitor1(IReportBuilder builder, int months) throws IOException
	{
		_months = months;
		_type = TraversingType.POSTORDER;
		setBuilder(builder);
		_currentSupply = 0;
	}
	/**
	 * Sets the report Builder that this visitor will use.
	 * @param builder any implementation of IReportBuilder.
	 * @throws IOException 
	 */
	private void setBuilder(IReportBuilder builder) throws IOException 
	{
		_builder = builder;
		String title = "Products";
		_builder.startTable(title, 4);
		_builder.addCell("Description");
		_builder.addCell("Barcode");
		_builder.addCell(_months + "-Month Supply");
		_builder.addCell("Current Supply");
	}

	/**
	 * Called when the Root Storage Unit is visited.
	 * @param rsu the RootStorageUnit.
	 */
	@Override
	public void visitRoot(RootStorageUnit rsu) {
		//do nothing

	}

	/**
	 * Called when a Storage Unit is visited
	 * @param su a reference to the visited StorageUnit
	 */
	@Override
	public void visitStorageUnit(StorageUnit su) {
		//do nothing

	}

	/**
	 * Called when a Product Group is visited
	 * @param pg a reference to the visited ProductGroup
	 */
	@Override
	public void visitProductGroup(ProductGroup pg) {
		//do nothing

	}

	/**
	 * Called when a Product is visited
	 * @param product a reference to the visited Product
	 */
	@Override
	public void visitProduct(Product product) 
	{
		float nMonthSupply = (product.getThreeMonthSupply()*_months)/3.0f;
		if (product.getThreeMonthSupply() == 0 ||
				nMonthSupply <= _currentSupply)
			return;
		try 
		{
			_builder.addCell(product.getDescription());
			_builder.addCell(product.getBarcode());
			_builder.addCell(getSupplyString(nMonthSupply));
			_builder.addCell(_currentSupply + " count");
		} 
		catch (IOException e) 
		{
			//this shouldn't happen
		}
		finally
		{
			_currentSupply = 0;
		}
	}

	private String getSupplyString(float nMonthSupply)
	{
		DecimalFormat df = new DecimalFormat("#0.##");
		return df.format(nMonthSupply) + " count";
	}
	/**
	 * Called when an Item is visited
	 * @param item a reference to the visited Item
	 */
	@Override
	public void visitItem(Item item) 
	{
		if (!item.isUsed())
			_currentSupply++;
	}

	@Override
	public TraversingType getType() {
		return _type;
	}

	@Override
	public void visitProductManager(ProductManager pm) {
		//do nothing
	}

	@Override
	public void visitItemManager(ItemManager im) {
		//do nothing
	}
	@Override
	public void onDone() {
		// TODO Auto-generated method stub
		
	}
}
